package org.example.deadletter;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

/**
 * 消息手动接收确认机制之死信队列
 *
 *@ClassName: MyAckReceiver
 *@Author: CJ
 *@Date: 2021-9-24 14:33
 */
@Slf4j
@Component
public class DeadLetterAckReceiver {

    @RabbitListener(queues = "dead.letter.business.queuea")
    @RabbitHandler
    public void deadLetterReceiver1(@NotNull Message message, Channel channel) {
        try {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), true, false);
            log.info("拒绝签收...消息的路由键为：" + message.getMessageProperties().getReceivedRoutingKey());
        } catch (Exception e) {
            log.info("消息拒绝签收失败", e);
        }
    }

    @RabbitListener(queues = "dead.letter.business.queueb")
    @RabbitHandler
    public void deadLetterReceiver2(@NotNull Message message, Channel channel) {
        try {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), true, false);
            log.info("拒绝签收...消息的路由键为：" + message.getMessageProperties().getReceivedRoutingKey());
        } catch (Exception e) {
            log.info("消息拒绝签收失败", e);
        }
    }
}
